﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using Dal;
using Model;

namespace Bll
{
    public class BllLayer
    {
        DalLayer dal = new DalLayer();
        /// <summary>
        /// 注册账号
        /// </summary>
        /// <param name="user"></param>
        /// <returns>成功返回1 学号已存在返回-2 账号已存在返回-1 失败返回0</returns>
        public int register(User_info user)
        {
            User_info ur = dal.GetUserInfo(user.SID);
            if(ur.account == "Null")
            {
                ur = dal.GetUserInfo(user.account, 0);
                if(ur.account == "Null")
                {
                    List<User_info> list = new List<User_info>();
                    list.Add(user);
                    if (dal.SetUserInfo(list) == 1)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return -1;
                }
               
            }
            return -2;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <returns>成功返回1 账号或学号不存在返回-1 数据库访问异常返回-2 密码错误返回0 未认证返回-3</returns>
        public int login(string account,string pwd)
        {
            User_info ur = dal.GetUserInfo(account);
            if (ur.account == "Null")
            {//是否是学号登录
                ur = dal.GetUserInfo(account, 0);
                if(ur.account == "Null")
                {//是否是账号登录
                    return -1;
                }else if(ur.account == "Error")
                {//数据库访问异常
                    return -2;
                }
                else
                {   if(ur.is_accepted == 0)
                    {
                        return -3;
                    }
                    if(ur.pwd == pwd)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
            else if(ur.account == "Error")
            {//数据库访问异常
                return -2;
            }
            {
                if (ur.is_accepted == 0)
                {
                    return -3;
                }
                if (ur.pwd == pwd)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }

        }
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="SID"></param>
        /// <returns></returns>
        public User_info GetUserInfo(string SID)
        {
            return dal.GetUserInfo(SID);
        }
        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        public List<User_info> GetUserInfos()
        {
            return dal.GetUser_Infos();
        }
        /// <summary>
        /// 用于各种对象的更新
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int Update(object obj)
        {
            if (obj.GetType() == typeof(User_info))
                return dal.UpdateUserInfo((User_info)obj);
            else if (obj.GetType() == typeof(Cart))
                return dal.UpdateCart((Cart)obj);
            else if (obj.GetType() == typeof(Goods_info))
                return dal.UpdateGoodsInfo((Goods_info)obj);
            else if (obj.GetType() == typeof(Order))
                return dal.UpdateOrder((Order)obj);
            else return -1;
        }

        /// <summary>
        /// 用于发送认证通知邮件
        /// </summary>
        /// <param name="type">类型 1 表示认证成功  0 表示认证失败</param>
        /// <param name="user">需要认证的用户对象</param>
        /// <returns>成功返回1 失败返回0</returns>
        public int UserIdentify(int type,User_info user)
        {
            string title = "慧存账号认证通知";
            string content = "您好"+user.name+":\n";
            if (type == 1)
            {//认证成功
                content += "   您已经成功通过认证，现在您可以正常登录我们的网站。";
                content += "<br/><br/>若您未进行过此操作，请无视。<br/><br/>该邮件为系统自动发送，无需回复";
                if (SendEmail(user.email, title, content) == 1)
                {
                    user.is_accepted = 1;
                    dal.UpdateUserInfo(user);
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            else if(type == 0)
            {
                content += "   您未通过我们的认证，可能是您的学生证照片不清晰或不符合规范造成的，请您确认后重新注册。";
                content += "<br/><br/>若您未进行过此操作，请无视。<br/><br/>该邮件为系统自动发送，无需回复";
                if (SendEmail(user.email, title, content) == 1)
                {
                   
                    dal.DeleteUserInfo(user.SID);
                    return 1;
                }
                else
                {
                    return 0;
                }
            }else if(type == 2)
            {
                content += "   您的认证已被取消，若想查明具体原因，请回复CM。";
               // content += "<br/><br/>该邮件为系统自动发送，无需回复";
                if (SendEmail(user.email, title, content) == 1)
                {
                    user.is_accepted = 0;
                    dal.UpdateUserInfo(user);
                    return 1;
                }
                else
                {
                    return 0;
                }

            }
            else
            {
                return 0;
            }
         
           
        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="addr">发送的地址</param>
        /// <param name="title">邮件的标题</param>
        /// <param name="content">邮件内容</param>
        /// <returns>1为成功 其它为失败</returns>
        private static int SendEmail(string addr, string title, string content)
        {
            string smtp = "smtp.163.com";
            string from = "HuiCun2021@163.com";
            string pwd = "OXYZAEYYEXJMMYAH";
            SmtpClient client = new SmtpClient();
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Host = smtp;
            client.Credentials = new System.Net.NetworkCredential(from, pwd);


            MailMessage message = new MailMessage(from, addr);
            message.Subject = title;
            message.Body = content;
            message.BodyEncoding = Encoding.UTF8;
            message.IsBodyHtml = true;
            try
            {

                client.Send(message);
                return 1;
            }
            catch (SmtpException ex)
            {
                return 0;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
    }
}
